<template>
  <div style="position: relative">
    <!-- 滑块验证 -->
    <el-card class="cover" v-if="loginAdmin.code">
      <slide-verify :l="42"
                    :r="10"
                    :w="310"
                    :h="155"
                    slider-text="向右滑动"
                    @success="onSuccess"
                    @fail="onFail"
                    @refresh="onRefresh"
      ></slide-verify>
    </el-card>

    <div style="width: 500px; height: 400px; background-color: aliceblue; border-radius: 10px; margin: 150px auto;">
      <div style="padding-top: 70px; text-align: center; font-size: xx-large; font-weight: bold; color: dodgerblue">管 理
        员 登 录
      </div>
      <el-form :model="admin" :rules="rules" ref="loginForm" style="padding: 50px; text-align: center">
        <el-form-item prop="account">
          <el-input placeholder="请输入管理员账号" prefix-icon="el-icon-user" size="medium" v-model="admin.account"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input placeholder="请输入管理员密码" show-password prefix-icon="el-icon-lock" size="medium"
                    v-model="admin.password"></el-input>
        </el-form-item>
        <el-button type="primary" @click="login">登录</el-button>
      </el-form>
    </div>
  </div>

</template>

<script>
import request from "@/utils/request";
import Cookies from "js-cookie";

export default {
  name: "Login",
  data() {
    return {
      loginAdmin: {},
      admin: {
        account: "admin",
        password: "00000a"
      },
      rules: {
        account: [
          {required: true, message: '请输入账号', trigger: 'blur'},
          {min: 1, max: 25, message: '长度在1-25位', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 25, message: '长度在6-25位', trigger: 'blur'}
        ],
      }
    }
  },
  methods: {
    login() {
      this.$refs['loginForm'].validate((valid) => {
        if (valid) {
          request.post('/admin/login', this.admin).then(res => {
            console.log(res);
            this.loginAdmin = res;
            //没有滑块之前应该这么写
            // if (res.code === '200') {
            //   this.$notify.success("登录成功");
            //   if (res.data !== null) {
            //     Cookies.set('admin', JSON.stringify(res.data));
            //   }
            //   this.$router.push('/');
            // } else {
            //   this.$notify.error(res.msg);
            // }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },
    onSuccess() {
      if (this.loginAdmin.code === '200') {
        this.$notify.success("登录成功");
        if (this.loginAdmin.data !== null) {
          Cookies.set('admin', JSON.stringify(this.loginAdmin.data));
        }
        this.$router.push('/');
      } else {
        this.$notify.error(this.loginAdmin.msg);
        this.loginAdmin = {};
        this.$router.push('/');
      }
    },
    onFail() {
    },
    onRefresh() {
      console.log('refresh')
    }
  }

}
</script>

<style scoped>
.cover {
  width: fit-content;
  background-color: white;
  position: absolute;
  top:50%;
  left:50%;
  transform: translate(-50%, -50%);
  z-index: 1000;
}
</style>